<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   【实战】机器学习系列(1)_逻辑回归初步  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/21334.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466460828: Accept with keywords: (title(0.25):逻辑,社区,回归,数盟, topn(0.7):概率,拟合,凸函数,数盟,类别,数据挖掘,代价,预测,边界,阈值,结果,Python,例子,参数,文章,数据,函数,python,回归,样本,决策,问题,算法,梯度下降,线性,最低点,逻辑,分类,人工智能,判定).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    【实战】机器学习系列(1)_逻辑回归初步
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    【实战】机器学习系列(1)_逻辑回归初步 | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,67] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech/ai">
       人工智能
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/21334.html">
          【实战】机器学习系列(1)_逻辑回归初步
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          1,366 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech/ai" rel="category tag">
          人工智能
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <h2>
        1、总述
       </h2>
       <p>
        逻辑回归是应用非常广泛的一个分类机器学习算法，它将数据拟合到一个logit函数(或者叫做logistic函数)中，从而能够完成对事件发生的概率进行预测。
       </p>
       <h2>
        2、由来
       </h2>
       <p>
        要说逻辑回归，我们得追溯到线性回归，想必大家对线性回归都有一定的了解，即对于多维空间中存在的样本点，我们用特征的线性组合去拟合空间中点的分布和轨迹。如下图所示：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014123301565?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        线性回归能对连续值结果进行预测，而现实生活中常见的另外一类问题是，分类问题。最简单的情况是是与否的二分类问题。比如说医生需要判断病人是否 生病，银行要判断一个人的信用程度是否达到可以给他发信用卡的程度，邮件收件箱要自动对邮件分类为正常邮件和垃圾邮件等等。
       </p>
       <p>
        当然，我们最直接的想法是，既然能够用线性回归预测出连续值结果，那根据结果设定一个阈值是不是就可以解决这个问题了呢？事实是，对于很标准的情 况，确实可以的，这里我们套用Andrew Ng老师的课件中的例子，下图中X为数据点肿瘤的大小，Y为观测结果是否是恶性肿瘤。通过构建线性回归模型，如hθ(x)所示，构建线性回归模型后，我们 设定一个阈值0.5，预测hθ(x)≥0.5的这些点为恶性肿瘤，而hθ(x)&lt;0.5为良性肿瘤。
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014123518573?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        但很多实际的情况下，我们需要学习的分类数据并没有这么精准，比如说上述例子中突然有一个不按套路出牌的数据点出现，如下图所示：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014123606004?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        你看，现在你再设定0.5，这个判定阈值就失效了，而现实生活的分类问题的数据，会比例子中这个更为复杂，而这个时候我们借助于线性回归+阈值的方式，已经很难完成一个鲁棒性很好的分类器了。
       </p>
       <p>
        在这样的场景下，逻辑回归就诞生了。它的核心思想是，如果线性回归的结果输出是一个连续值，而值的范围是无法限定的，那我们有没有办法把这个结果 值映射为可以帮助我们判断的结果呢。而如果输出结果是 (0,1) 的一个概率值，这个问题就很清楚了。我们在数学上找了一圈，还真就找着这样一个简单的函数了，就是很神奇的sigmoid函数(如下)：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014123727818?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        如果把sigmoid函数图像画出来，是如下的样子：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124034991?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        Sigmoid Logistic Function
       </p>
       <p>
        从函数图上可以看出，函数y=g(z)在z=0的时候取值为1/2，而随着z逐渐变小，函数值趋于0，z逐渐变大的同时函数值逐渐趋于1，而这正是一个概率的范围。
       </p>
       <p>
        所以我们定义线性回归的预测函数为Y=W
        <sup>
         T
        </sup>
        X，那么逻辑回归的输出Y= g(W
        <sup>
         T
        </sup>
        X)，其中y=g(z)函数正是上述sigmoid函数(或者简单叫做S形函数)。
       </p>
       <h2>
        3、判定边界
       </h2>
       <p>
        我们现在再来看看，为什么逻辑回归能够解决分类问题。这里引入一个概念，叫做判定边界，可以理解为是用以对不同类别的数据分割的边界，边界的两旁应该是不同类别的数据。
       </p>
       <p>
        从二维直角坐标系中，举几个例子，大概是如下这个样子：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124124641?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        有时候是这个样子：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124156527?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        甚至可能是这个样子：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124241795?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        上述三幅图中的红绿样本点为不同类别的样本，而我们划出的线，不管是直线、圆或者是曲线，都能比较好地将图中的两类样本分割开来。这就是我们的判定边界，下面我们来看看，逻辑回归是如何根据样本点获得这些判定边界的。
       </p>
       <p>
        我们依旧借用Andrew Ng教授的课程中部分例子来讲述这个问题。
       </p>
       <p>
        回到sigmoid函数，我们发现：
       </p>
       <p>
        当
        <span id="MathJax-Element-18-Frame">
         <span id="MathJax-Span-242">
          <span id="MathJax-Span-243">
           <span id="MathJax-Span-244">
            g
           </span>
           <span id="MathJax-Span-245">
            (
           </span>
           <span id="MathJax-Span-246">
            z
           </span>
           <span id="MathJax-Span-247">
            )
           </span>
           <span id="MathJax-Span-248">
            ≥
           </span>
           <span id="MathJax-Span-249">
            0.5
           </span>
          </span>
         </span>
        </span>
        时,
        <span id="MathJax-Element-19-Frame">
         <span id="MathJax-Span-250">
          <span id="MathJax-Span-251">
           <span id="MathJax-Span-252">
            z
           </span>
           <span id="MathJax-Span-253">
            ≥
           </span>
           <span id="MathJax-Span-254">
            0
           </span>
          </span>
         </span>
        </span>
        ;
       </p>
       <p>
        对于
        <span id="MathJax-Element-20-Frame">
         <span id="MathJax-Span-255">
          <span id="MathJax-Span-256">
           <span id="MathJax-Span-257">
            <span id="MathJax-Span-258">
             h
            </span>
           </span>
          </span>
          <span id="MathJax-Span-259">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-260">
         (
        </span>
        <span id="MathJax-Span-261">
         x
        </span>
        <span id="MathJax-Span-262">
         )
        </span>
        <span id="MathJax-Span-263">
         =
        </span>
        <span id="MathJax-Span-264">
         g
        </span>
        <span id="MathJax-Span-265">
         (
        </span>
        <span id="MathJax-Span-266">
         <span id="MathJax-Span-267">
         </span>
        </span>
        θ
        <sup>
         T
        </sup>
        X
        <span id="MathJax-Span-269">
        </span>
        <span id="MathJax-Span-270">
         )
        </span>
        <span id="MathJax-Span-271">
         ≥
        </span>
        <span id="MathJax-Span-272">
         0.5
        </span>
        , 则
        <span id="MathJax-Element-21-Frame">
         <span id="MathJax-Span-276">
         </span>
        </span>
        θ
        <sup>
         T
        </sup>
        X
        <span id="MathJax-Span-278">
        </span>
        <span id="MathJax-Span-279">
         ≥
        </span>
        <span id="MathJax-Span-280">
         0
        </span>
        , 此时意味着预估y=1;
       </p>
       <p>
        反之，当预测y = 0时，
        <span id="MathJax-Element-22-Frame">
         <span id="MathJax-Span-284">
         </span>
        </span>
        θ
        <sup>
         T
        </sup>
        X
        <span id="MathJax-Span-286">
        </span>
        <span id="MathJax-Span-287">
         &lt;
        </span>
        <span id="MathJax-Span-288">
         0
        </span>
        ;
       </p>
       <p>
        所以我们认为θ
        <sup>
         T
        </sup>
        X =0是一个决策边界，当它大于0或小于0时，逻辑回归模型分别预测不同的分类结果。
       </p>
       <p>
        先看第一个例子
        <span id="MathJax-Span-296">
         h
        </span>
        <span id="MathJax-Span-299">
         θ
        </span>
        <span id="MathJax-Span-300">
         (
        </span>
        <span id="MathJax-Span-301">
         x
        </span>
        <span id="MathJax-Span-302">
         )
        </span>
        <span id="MathJax-Span-303">
         =
        </span>
        <span id="MathJax-Span-304">
         g
        </span>
        <span id="MathJax-Span-305">
         (
        </span>
        θ
        <sub>
         0
        </sub>
        +θ
        <sub>
         1
        </sub>
        X
        <sub>
         1
        </sub>
        +θ
        <sub>
         2
        </sub>
        X
        <sub>
         2
        </sub>
        <span id="MathJax-Span-323">
         )
        </span>
        ，其中
        <span id="MathJax-Element-25-Frame">
         <span id="MathJax-Span-324">
          <span id="MathJax-Span-325">
           <span id="MathJax-Span-326">
            <span id="MathJax-Span-327">
            </span>
           </span>
          </span>
         </span>
        </span>
        θ
        <sub>
         0 ,
        </sub>
        θ
        <sub>
         1 ,
        </sub>
        θ
        <sub>
         2
        </sub>
        分别取-3, 1, 1。则当−3+X
        <sub>
         1
        </sub>
        +X
        <sub>
         2
        </sub>
        <span id="MathJax-Span-349">
         ≥
        </span>
        <span id="MathJax-Span-350">
         0
        </span>
        时, y = 1; 则
        <span id="MathJax-Span-353">
         <span id="MathJax-Span-354">
         </span>
        </span>
        X
        <sub>
         1
        </sub>
        +X
        <sub>
         2
        </sub>
        <span id="MathJax-Span-359">
        </span>
        <span id="MathJax-Span-363">
         =
        </span>
        <span id="MathJax-Span-364">
         3
        </span>
        是一个决策边界，图形表示如下，刚好把图上的两类点区分开来：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124638710?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        例1只是一个线性的决策边界，当
        <span id="MathJax-Element-28-Frame">
         <span id="MathJax-Span-365">
          <span id="MathJax-Span-366">
           <span id="MathJax-Span-367">
            <span id="MathJax-Span-368">
             h
            </span>
           </span>
          </span>
          <span id="MathJax-Span-369">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-370">
         (
        </span>
        <span id="MathJax-Span-371">
         x
        </span>
        <span id="MathJax-Span-372">
         )
        </span>
        更复杂的时候，我们可以得到非线性的决策边界，例如：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124838937?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        这时当x
        <sub>
         1
        </sub>
        <sup>
         2
        </sup>
        +x
        <sub>
         2
        </sub>
        <sup>
         2
        </sup>
        ≥1时，我们判定y=1，这时的决策边界是一个圆形，如下图所示：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014124912098?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        所以我们发现，理论上说，只要我们的
        <span id="MathJax-Element-28-Frame">
         <span id="MathJax-Span-365">
          <span id="MathJax-Span-366">
           <span id="MathJax-Span-367">
            <span id="MathJax-Span-368">
             h
            </span>
           </span>
          </span>
          <span id="MathJax-Span-369">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-370">
         (
        </span>
        <span id="MathJax-Span-371">
         x
        </span>
        <span id="MathJax-Span-372">
         )
        </span>
        设计足够合理，准确的说是
        <span id="MathJax-Span-264">
         g
        </span>
        <span id="MathJax-Span-265">
         (
        </span>
        <span id="MathJax-Span-266">
         θ
        </span>
        <span id="MathJax-Span-268">
         T
        </span>
        <span id="MathJax-Span-269">
         x
        </span>
        <span id="MathJax-Span-270">
         )中
         <span id="MathJax-Span-266">
          <span id="MathJax-Span-267">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-268">
         T
        </span>
        <span id="MathJax-Span-269">
         x
        </span>
        足够复杂，我们能在不同的情形下，拟合出不同的判定边界，从而把不同的样本点分隔开来。
       </p>
       <h2>
        4、代价函数与梯度下降
       </h2>
       <p>
        我们通过对判定边界的说明，知道会有合适的参数θ使得
        <span id="MathJax-Span-266">
         θ
        </span>
        <span id="MathJax-Span-268">
         T
        </span>
        <span id="MathJax-Span-269">
         x=0成为很好的分类判定边界，那么问题就来了，我们如何判定我们的参数θ是否合适，有多合适呢？更进一步，我们有没有办法去求得这样的合适参数θ呢？
        </span>
       </p>
       <p>
        这就是我们要提到的代价函数与梯度下降了。
       </p>
       <p>
        所谓的代价函数Cost Function，其实是一种衡量我们在这组参数下预估的结果和实际结果差距的函数，比如说线性回归的代价函数定义为:
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125116151?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        当然我们可以和线性回归类比得到一个代价函数，实际就是上述公式中
        <span id="MathJax-Span-577">
         h
        </span>
        <span id="MathJax-Span-579">
         θ
        </span>
        <span id="MathJax-Span-580">
         (
        </span>
        <span id="MathJax-Span-581">
         x
        </span>
        <span id="MathJax-Span-582">
         )取为逻辑回归中的
         <span id="MathJax-Span-264">
          g
         </span>
         <span id="MathJax-Span-265">
          (
         </span>
         <span id="MathJax-Span-266">
          <span id="MathJax-Span-267">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-268">
         T
        </span>
        <span id="MathJax-Span-269">
         x
        </span>
        <span id="MathJax-Span-270">
         )
        </span>
        ，但是这会引发代价函数为“非凸”函数的问题，简单一点说就是这个函数有很多个局部最低点，如下图所示：
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=607435295049781725"/>
       </p>
       <p>
        而我们希望我们的代价函数是一个如下图所示，碗状结构的凸函数，这样我们算法求解到局部最低点，就一定是全局最小值点。
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=847516551720124317"/>
       </p>
       <p>
        因此，上述的Cost Function对于逻辑回归是不可行的，我们需要其他形式的Cost Function来保证逻辑回归的成本函数是凸函数。
       </p>
       <p>
        我们跳过大量的数学推导，直接出结论了，我们找到了一个适合逻辑回归的代价函数:
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014130511356?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        Andrew Ng老师解释了一下这个代价函数的合理性，我们首先看当y=1的情况：
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=7499905772199633281"/>
       </p>
       <p>
        如果我们的类别y = 1, 而判定的
        <span id="MathJax-Element-43-Frame">
         <span id="MathJax-Span-712">
          <span id="MathJax-Span-713">
           <span id="MathJax-Span-714">
            <span id="MathJax-Span-715">
             h
            </span>
           </span>
          </span>
          <span id="MathJax-Span-716">
           θ
          </span>
         </span>
        </span>
        <span id="MathJax-Span-717">
         (
        </span>
        <span id="MathJax-Span-718">
         x
        </span>
        <span id="MathJax-Span-719">
         )
        </span>
        <span id="MathJax-Span-720">
         =
        </span>
        <span id="MathJax-Span-721">
         1
        </span>
        ，则Cost = 0，此时预测的值和真实的值完全相等，代价本该为0；而如果判断
        <span id="MathJax-Span-724">
         h
        </span>
        <span id="MathJax-Span-726">
         θ
        </span>
        <span id="MathJax-Span-727">
         (
        </span>
        <span id="MathJax-Span-728">
         x
        </span>
        <span id="MathJax-Span-729">
         )
        </span>
        <span id="MathJax-Span-730">
         →
        </span>
        <span id="MathJax-Span-731">
         0
        </span>
        ，代价-&gt;∞，这很好地惩罚了最后的结果。
       </p>
       <p>
        而对于y=0的情况，如下图所示，也同样合理：
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=16991899942735763470"/>
       </p>
       <p>
        下面我们说说梯度下降，梯度下降算法是调整参数θ使得代价函数J(θ)取得最小值的最基本方法之一。从直观上理解，就是我们在碗状结构的凸函数上取一个初始值，然后挪动这个值一步步靠近最低点的过程，如下图所示：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125344499?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        我们先简化一下逻辑回归的代价函数：
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=4662816715826976375"/>
       </p>
       <p>
        从数学上理解，我们为了找到最小值点，就应该朝着下降速度最快的方向(导函数/偏导方向)迈进，每次迈进一小步，再看看此时的下降最快方向是哪，再朝着这个方向迈进，直至最低点。
       </p>
       <p>
        用迭代公式表示出来的最小化
        <span id="MathJax-Element-52-Frame">
         <span id="MathJax-Span-794">
          <span id="MathJax-Span-795">
           <span id="MathJax-Span-796">
            J
           </span>
          </span>
          <span id="MathJax-Span-797">
           (
          </span>
          <span id="MathJax-Span-798">
           θ
          </span>
         </span>
         <span id="MathJax-Span-799">
          )
         </span>
        </span>
        的梯度下降算法如下：
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=5185913255970284499"/>
       </p>
       <p>
        <img src="http://52opencourse.com/?qa=blob&amp;qa_blobid=14303327028035669672"/>
       </p>
       <h2>
        5、代码与实现
       </h2>
       <p>
        我们来一起看两个具体数据上做逻辑回归分类的例子，其中一份数据为线性判定边界，另一份为非线性。
       </p>
       <p>
        <strong>
         示例1。
        </strong>
       </p>
       <p>
        第一份数据为data1.txt，部分内容如下：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125459234?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        我们先来看看数据在空间的分布，代码如下。
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a980625c215700644" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             from numpy import loadtxt, where
from pylab import scatter, show, legend, xlabel, ylabel

#load the dataset
data = loadtxt('/home/HanXiaoyang/data/data1.txt', delimiter=',')

X = data[:, 0:2]
y = data[:, 2]
pos = where(y == 1)
neg = where(y == 0)

scatter(X[pos, 0], X[pos, 1], marker='o', c='b')
scatter(X[neg, 0], X[neg, 1], marker='x', c='r')
xlabel('Feature1/Exam 1 score')
ylabel('Feature2/Exam 2 score')

legend(['Fail', 'Pass'])

show()
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-15">
                  15
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-16">
                  16
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-17">
                  17
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980625c215700644-18">
                  18
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980625c215700644-19">
                  19
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a980625c215700644-1">
                  <span class="crayon-st">
                   from
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   numpy
                  </span>
                  <span class="crayon-r">
                   import
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   loadtxt
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   where
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-2">
                  <span class="crayon-st">
                   from
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   pylab
                  </span>
                  <span class="crayon-r">
                   import
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   scatter
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   show
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   legend
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   xlabel
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   ylabel
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-3">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-4">
                  <span class="crayon-c">
                   #load the dataset
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-5">
                  <span class="crayon-v">
                   data
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   loadtxt
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   '/home/HanXiaoyang/data/data1.txt'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delimiter
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-s">
                   ','
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-6">
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-7">
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   data
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-cn">
                   2
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-8">
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   data
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   2
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-9">
                  <span class="crayon-v">
                   pos
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   where
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   ==
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-10">
                  <span class="crayon-v">
                   neg
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   where
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   ==
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-11">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-12">
                  <span class="crayon-e">
                   scatter
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   pos
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   pos
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   marker
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-s">
                   'o'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   c
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-s">
                   'b'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-13">
                  <span class="crayon-e">
                   scatter
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   neg
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   neg
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   marker
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-s">
                   'x'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   c
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-s">
                   'r'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-14">
                  <span class="crayon-e">
                   xlabel
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   'Feature1/Exam 1 score'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-15">
                  <span class="crayon-e">
                   ylabel
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   'Feature2/Exam 2 score'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-16">
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-17">
                  <span class="crayon-e">
                   legend
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-s">
                   'Fail'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   'Pass'
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980625c215700644-18">
                 </div>
                 <div class="crayon-line" id="crayon-57686a980625c215700644-19">
                  <span class="crayon-e">
                   show
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0345 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        得到的结果如下：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125551129?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        下面我们写好计算sigmoid函数、代价函数、和梯度下降的程序：
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a980626f905068244" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             def sigmoid(X):
    '''''Compute sigmoid function '''
    den =1.0+ e **(-1.0* X)
    gz =1.0/ den
    return gz

def compute_cost(theta,X,y):
    '''''computes cost given predicted and actual values'''
    m = X.shape[0]#number of training examples
    theta = reshape(theta,(len(theta),1))
    J =(1./m)*(-transpose(y).dot(log(sigmoid(X.dot(theta))))- transpose(1-y).dot(log(1-sigmoid(X.dot(theta)))))
    grad = transpose((1./m)*transpose(sigmoid(X.dot(theta))- y).dot(X))
    #optimize.fmin expects a single value, so cannot return grad
    return J[0][0]#,grad

def compute_grad(theta, X, y):
    '''''compute gradient'''
    theta.shape =(1,3)
    grad = zeros(3)
    h = sigmoid(X.dot(theta.T))
    delta = h - y
    l = grad.size
    for i in range(l):
        sumdelta = delta.T.dot(X[:, i])
        grad[i]=(1.0/ m)* sumdelta *-1
    theta.shape =(3,)
    return  grad
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-15">
                  15
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-16">
                  16
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-17">
                  17
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-18">
                  18
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-19">
                  19
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-20">
                  20
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-21">
                  21
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-22">
                  22
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-23">
                  23
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-24">
                  24
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-25">
                  25
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980626f905068244-26">
                  26
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980626f905068244-27">
                  27
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a980626f905068244-1">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-2">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''Compute sigmoid function '''
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-3">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   den
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   e
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-4">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   gz
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   den
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-5">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   gz
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-6">
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-7">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   compute_cost
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-8">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''computes cost given predicted and actual values'''
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-9">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-c">
                   #number of training examples
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-10">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   reshape
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   len
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-11">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   J
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-e">
                   transpose
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   log
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   transpose
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   log
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-12">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   transpose
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-e">
                   transpose
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-13">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-c">
                   #optimize.fmin expects a single value, so cannot return grad
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-14">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   J
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-c">
                   #,grad
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-15">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-16">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   compute_grad
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-17">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''compute gradient'''
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-18">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   3
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-19">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   3
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-20">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-21">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   y
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-22">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   size
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-23">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   i
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-24">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   sumdelta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-25">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   sumdelta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980626f905068244-26">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   3
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980626f905068244-27">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0430 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        我们用梯度下降算法得到的结果判定边界是如下的样子：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125624067?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        最后我们使用我们的判定边界对training data做一个预测，然后比对一下准确率：
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a980628d215672096" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             def predict(theta, X):
    '''''Predict label using learned logistic regression parameters'''
    m, n = X.shape
    p = zeros(shape=(m,1))
    h = sigmoid(X.dot(theta.T))
    for it in range(0, h.shape[0]):
        if h[it]&gt;0.5:
            p[it,0]=1
        else:
            p[it,0]=0
    return p

#Compute accuracy on our training set
p = predict(array(theta), it)
print 'Train Accuracy: %f'%((y[where(p == y)].size / float(y.size))*100.0)
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a980628d215672096-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a980628d215672096-15">
                  15
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a980628d215672096-1">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   predict
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-2">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''Predict label using learned logistic regression parameters'''
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-3">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   n
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-i">
                   shape
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-4">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-5">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-6">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   it
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-7">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   if
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   &gt;
                  </span>
                  <span class="crayon-cn">
                   0.5
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-8">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-9">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   else
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-10">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-11">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-12">
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-13">
                  <span class="crayon-c">
                   #Compute accuracy on our training set
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a980628d215672096-14">
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   predict
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   array
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a980628d215672096-15">
                  <span class="crayon-k ">
                   print
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   'Train Accuracy: %f'
                  </span>
                  <span class="crayon-o">
                   %
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-e">
                   where
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   ==
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   float
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-cn">
                   100.0
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0231 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        计算出来的结果是89.2%
       </p>
       <p>
        <strong>
         示例2.
        </strong>
       </p>
       <p>
        第二份数据为data2.txt，部分内容如下：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125654926?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        我们同样把数据的分布画出来，如下：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125725759?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        我们发现在这个例子中，我们没有办法再用一条直线把两类样本点近似分开了，所以我们打算试试多项式的判定边界，那么我们先要对给定的两个feature做一个多项式特征的映射。比如说，我们做了如下的一个映射：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125802837?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        代码如下：
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a9806299430644901" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             def map_feature(x1, x2):
    '''''
    Maps the two input features to polonomial features.
    Returns a new feature array with more features of
    X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc...
    '''
    x1.shape =(x1.size,1)
    x2.shape =(x2.size,1)
    degree =6
    mapped_fea = ones(shape=(x1[:,0].size,1))
    m, n = mapped_fea.shape
    for i in range(1, degree +1):
        for j in range(i +1):
            r =(x1 **(i - j))*(x2 ** j)
            mapped_fea = append(out, r, axis=1)
    return mapped_fea

mapped_fea = map_feature(X[:,0], X[:,1])
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-15">
                  15
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-16">
                  16
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a9806299430644901-17">
                  17
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a9806299430644901-18">
                  18
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a9806299430644901-1">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   map_feature
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x1
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   x2
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-2">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-3">
                  <span class="crayon-s">
                   Maps the two input features to polonomial features.
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-4">
                  <span class="crayon-s">
                   Returns a new feature array with more features of
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-5">
                  <span class="crayon-s">
                   X1, X2, X1 ** 2, X2 ** 2, X1*X2, X1*X2 ** 2, etc...
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-6">
                  <span class="crayon-s">
                   '''
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-7">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   x1
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x1
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-8">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   x2
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x2
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-9">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   degree
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   6
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-10">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   mapped_fea
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   ones
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x1
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-11">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   n
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   mapped_fea
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   shape
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-12">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   i
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   degree
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-13">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   j
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-14">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   r
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x1
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   j
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   x2
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   j
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-15">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   mapped_fea
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   append
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   out
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   r
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   axis
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-16">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   mapped_fea
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a9806299430644901-17">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a9806299430644901-18">
                  <span class="crayon-v">
                   mapped_fea
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   map_feature
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0157 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        接着做梯度下降：
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a98062a0543522285" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             def cost_function_reg(theta, X, y, l):
    '''''Compute the cost and partial derivatives as grads
    '''
    h = sigmoid(X.dot(theta))
    thetaR = theta[1:,0]
    J =(1.0/ m)*((-y.T.dot(log(h)))-((1- y.T).dot(log(1.0- h)))) \
            +(l /(2.0* m))*(thetaR.T.dot(thetaR))
    delta = h - y
    sum_delta = delta.T.dot(X[:,1])
    grad1 =(1.0/ m)* sumdelta
    XR = X[:,1:X.shape[1]]
    sum_delta = delta.T.dot(XR)
    grad =(1.0/ m)*(sum_delta + l * thetaR)
    out = zeros(shape=(grad.shape[0], grad.shape[1]+1))
    out[:,0]= grad1
    out[:,1:]= grad
    return J.flatten(), out.T.flatten()

m, n = X.shape
y.shape =(m,1)
it = map_feature(X[:,0], X[:,1])
#Initialize theta parameters
initial_theta = zeros(shape=(it.shape[1],1))
#Use regularization and set parameter lambda to 1
l =1
# Compute and display initial cost and gradient for regularized logistic
# regression
cost, grad = cost_function_reg(initial_theta, it, y, l)

def decorated_cost(theta):
    return cost_function_reg(theta, it, y, l)

print fmin_bfgs(decorated_cost, initial_theta, maxfun=500)
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-15">
                  15
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-16">
                  16
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-17">
                  17
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-18">
                  18
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-19">
                  19
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-20">
                  20
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-21">
                  21
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-22">
                  22
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-23">
                  23
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-24">
                  24
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-25">
                  25
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-26">
                  26
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-27">
                  27
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-28">
                  28
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-29">
                  29
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-30">
                  30
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-31">
                  31
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a0543522285-32">
                  32
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a0543522285-33">
                  33
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-1">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   cost_function_reg
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-2">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''Compute the cost and partial derivatives as grads
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-3">
                  <span class="crayon-s">
                   '''
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-4">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-5">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   thetaR
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-6">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   J
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   log
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   log
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-sy">
                   \
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-7">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   2.0
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   thetaR
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   thetaR
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-8">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   y
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-9">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   sum_delta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-10">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad1
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sumdelta
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-11">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-v">
                   XR
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-12">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   sum_delta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   delta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   XR
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-13">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   1.0
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   sum_delta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   thetaR
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-14">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   out
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   +
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-15">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   out
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   grad1
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-16">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-v">
                   out
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   grad
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-17">
                  <span class="crayon-e">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   J
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   flatten
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   out
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   flatten
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-18">
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-19">
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   n
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-i">
                   shape
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-20">
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-21">
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   map_feature
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-22">
                  <span class="crayon-c">
                   #Initialize theta parameters
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-23">
                  <span class="crayon-v">
                   initial_theta
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-24">
                  <span class="crayon-c">
                   #Use regularization and set parameter lambda to 1
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-25">
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-26">
                  <span class="crayon-c">
                   # Compute and display initial cost and gradient for regularized logistic
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-27">
                  <span class="crayon-c">
                   # regression
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-28">
                  <span class="crayon-v">
                   cost
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   grad
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   cost_function_reg
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   initial_theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-29">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-30">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   decorated_cost
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-31">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   cost_function_reg
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a0543522285-32">
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a0543522285-33">
                  <span class="crayon-k ">
                   print
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   fmin_bfgs
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   decorated_cost
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   initial_theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   maxfun
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   500
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0590 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        接着在数据点上画出判定边界：
       </p>
       <div class="dp-highlighter bg_python">
        <div class="bar">
         <div class="tools">
          <p>
           <strong>
            [python]
           </strong>
           <a class="ViewSource" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="view plain">
            view plain
           </a>
           <a class="CopyToClipboard" href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank" title="copy">
            copy
           </a>
          </p>
          <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
          <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686a98062a9201371342" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
           <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
            <span class="crayon-title">
            </span>
            <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
             <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-wrap-button" title="切换自动换行">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-expand-button" title="点击展开代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-copy-button" title="复制代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
              <div class="crayon-button-icon">
              </div>
             </div>
             <span class="crayon-language">
              Python
             </span>
            </div>
           </div>
           <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
           </div>
           <div class="crayon-plain-wrap">
            <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
             #Plot Boundary
u = linspace(-1,1.5,50)
v = linspace(-1,1.5,50)
z = zeros(shape=(len(u), len(v)))
for i in range(len(u)):
    for j in range(len(v)):
        z[i, j]=(map_feature(array(u[i]), array(v[j])).dot(array(theta)))
z = z.T
contour(u, v, z)
title('lambda = %f'% l)
xlabel('Microchip Test 1')
ylabel('Microchip Test 2')
legend(['y = 1','y = 0','Decision boundary'])
show()

def predict(theta, X):
    '''''Predict whether the label
    is 0 or 1 using learned logistic
    regression parameters '''
    m, n = X.shape
    p = zeros(shape=(m,1))
    h = sigmoid(X.dot(theta.T))
    for it in range(0, h.shape[0]):
        if h[it]&gt;0.5:
            p[it,0]=1
        else:
            p[it,0]=0
    return p

#% Compute accuracy on our training set
p = predict(array(theta), it)
print'Train Accuracy: %f'%((y[where(p == y)].size / float(y.size))*100.0)
            </textarea>
           </div>
           <div class="crayon-main" style="">
            <table class="crayon-table">
             <tbody>
              <tr class="crayon-row">
               <td class="crayon-nums " data-settings="show">
                <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-1">
                  1
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-2">
                  2
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-3">
                  3
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-4">
                  4
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-5">
                  5
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-6">
                  6
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-7">
                  7
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-8">
                  8
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-9">
                  9
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-10">
                  10
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-11">
                  11
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-12">
                  12
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-13">
                  13
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-14">
                  14
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-15">
                  15
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-16">
                  16
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-17">
                  17
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-18">
                  18
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-19">
                  19
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-20">
                  20
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-21">
                  21
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-22">
                  22
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-23">
                  23
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-24">
                  24
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-25">
                  25
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-26">
                  26
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-27">
                  27
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-28">
                  28
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-29">
                  29
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-30">
                  30
                 </div>
                 <div class="crayon-num" data-line="crayon-57686a98062a9201371342-31">
                  31
                 </div>
                 <div class="crayon-num crayon-striped-num" data-line="crayon-57686a98062a9201371342-32">
                  32
                 </div>
                </div>
               </td>
               <td class="crayon-code">
                <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-1">
                  <span class="crayon-c">
                   #Plot Boundary
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-2">
                  <span class="crayon-v">
                   u
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   linspace
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1.5
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   50
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-3">
                  <span class="crayon-v">
                   v
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   linspace
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-o">
                   -
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1.5
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   50
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-4">
                  <span class="crayon-v">
                   z
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   len
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   u
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   len
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   v
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-5">
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   i
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   len
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   u
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-6">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-i">
                   j
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   len
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   v
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-7">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   z
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   j
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-e">
                   map_feature
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   array
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   u
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   i
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   array
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   v
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   j
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   array
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-8">
                  <span class="crayon-v">
                   z
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   z
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-i">
                   T
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-9">
                  <span class="crayon-e">
                   contour
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   u
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   v
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   z
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-10">
                  <span class="crayon-e">
                   title
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   'lambda = %f'
                  </span>
                  <span class="crayon-o">
                   %
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   l
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-11">
                  <span class="crayon-e">
                   xlabel
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   'Microchip Test 1'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-12">
                  <span class="crayon-e">
                   ylabel
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-s">
                   'Microchip Test 2'
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-13">
                  <span class="crayon-e">
                   legend
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-s">
                   'y = 1'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-s">
                   'y = 0'
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-s">
                   'Decision boundary'
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-14">
                  <span class="crayon-e">
                   show
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-15">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-16">
                  <span class="crayon-r">
                   def
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   predict
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-17">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-s">
                   '''''Predict whether the label
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-18">
                  <span class="crayon-s">
                   is 0 or 1 using learned logistic
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-19">
                  <span class="crayon-s">
                   regression parameters '''
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-20">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   n
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-i">
                   shape
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-21">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   zeros
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   m
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-22">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   sigmoid
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   X
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-e">
                   dot
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   T
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-23">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   for
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   it
                  </span>
                  <span class="crayon-st">
                   in
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   range
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   shape
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-24">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   if
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   h
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   &gt;
                  </span>
                  <span class="crayon-cn">
                   0.5
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-25">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   1
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-26">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   else
                  </span>
                  <span class="crayon-o">
                   :
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-27">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-cn">
                   0
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-28">
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-st">
                   return
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-29">
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-30">
                  <span class="crayon-c">
                   #% Compute accuracy on our training set
                  </span>
                 </div>
                 <div class="crayon-line" id="crayon-57686a98062a9201371342-31">
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   =
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-e">
                   predict
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-k ">
                   array
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   theta
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ,
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   it
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                 <div class="crayon-line crayon-striped-line" id="crayon-57686a98062a9201371342-32">
                  <span class="crayon-k ">
                   print
                  </span>
                  <span class="crayon-s">
                   'Train Accuracy: %f'
                  </span>
                  <span class="crayon-o">
                   %
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   [
                  </span>
                  <span class="crayon-e">
                   where
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   p
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   ==
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   ]
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-o">
                   /
                  </span>
                  <span class="crayon-h">
                  </span>
                  <span class="crayon-k ">
                   float
                  </span>
                  <span class="crayon-sy">
                   (
                  </span>
                  <span class="crayon-v">
                   y
                  </span>
                  <span class="crayon-sy">
                   .
                  </span>
                  <span class="crayon-v">
                   size
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                  <span class="crayon-o">
                   *
                  </span>
                  <span class="crayon-cn">
                   100.0
                  </span>
                  <span class="crayon-sy">
                   )
                  </span>
                 </div>
                </div>
               </td>
              </tr>
             </tbody>
            </table>
           </div>
          </div>
          <!-- [Format Time: 0.0455 seconds] -->
          <p>
          </p>
         </div>
        </div>
       </div>
       <p>
        得到的结果如下图所示：
       </p>
       <p>
        <img src="http://img.blog.csdn.net/20151014125838349?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center"/>
       </p>
       <p>
        我们发现我们得到的这条曲线确实将两类点区分开来了。
       </p>
       <h2>
        6、总结
       </h2>
       <p>
        最后我们总结一下逻辑回归。它始于输出结果为有实际意义的连续值的线性回归，但是线性回归对于分类的问题没有办法准确而又具备鲁棒性地分割，因此我们设计出了逻辑回归这样一个算法，它的输出结果表征了某个样本属于某类别的概率。
       </p>
       <p>
        逻辑回归的成功之处在于，将原本输出结果范围可以非常大的θ
        <sup>
         T
        </sup>
        X 通过sigmoid函数映射到(0,1)，从而完成概率的估测。
       </p>
       <p>
        而直观地在二维空间理解逻辑回归，是sigmoid函数的特性，使得判定的阈值能够映射为平面的一条判定边界，当然随着特征的复杂化，判定边界可能是多种多样的样貌，但是它能够较好地把两类样本点分隔开，解决分类问题。
       </p>
       <p>
        求解逻辑回归参数的传统方法是梯度下降，构造为凸函数的代价函数后，每次沿着偏导方向(下降速度最快方向)迈进一小部分，直至N次迭代后到达最低点。
       </p>
       <p>
        <strong>
         出处：
        </strong>
       </p>
       <p>
        <a href="http://blog.csdn.net/han_xiaoyang/article/details/49123419" target="_blank">
         http://blog.csdn.net/han_xiaoyang/article/details/49123419
        </a>
        。
       </p>
       <p>
        <a href="http://blog.csdn.net/longxinchen_ml/article/details/49130931" target="_blank">
         http://blog.csdn.net/longxinchen_ml/article/details/49130931
        </a>
        。
       </p>
       <h2>
        作者介绍
       </h2>
       <p>
        龙心尘和寒小阳：专注机器学习、数据挖掘 『我们是一群热爱机器学习，喜欢交流分享的小伙伴，希望通过“ML学分计划”交流机器学习相关的知识，认识更多的朋友。欢迎大家加入我们的讨论群获取资源资料，交流和分享。』 联系方式： 龙心尘 johnnygong.ml@gmail.com 寒小阳 hanxiaoyang.ml@gmail.com
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/24663.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/095356be8cb4z9ke9389kd-204x200.jpeg"/>
        </a>
        <a class="link" href="http://dataunion.org/24663.html" rel="bookmark" title="人工智能界最强大脑畅谈AI未来之路">
         人工智能界最强大脑畅谈AI未来之路
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24652.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/02/20150522141422337-265x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24652.html" rel="bookmark" title="重磅 | MIT与谷歌专家合著论文：机器学习和神经科学的相互启发与融合（附论文）">
         重磅 | MIT与谷歌专家合著论文：机器学习和神经科学的相互启发与融合（附论文）
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24616.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/224806wllbe6tcimyyi9xt-300x167.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24616.html" rel="bookmark" title="微软人工智能计划背后的华人力量">
         微软人工智能计划背后的华人力量
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/24562.html">
         <img src="http://dataunion.org/wp-content/uploads/2016/06/8aac7ceb921d1684f06e6f65e2c1f224-300x173.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/24562.html" rel="bookmark" title="各位大佬，别再拿人工智能当春药了！">
         各位大佬，别再拿人工智能当春药了！
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <ul id="comment">
      </ul>
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F21334.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/21334.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/21334.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>